//
//  EXIFViewController.m
//  Paparazzi
//
//  Created by Eric Williams on 11/5/10.
//  Copyright 2010 UCLA. All rights reserved.
//

#import "EXIFViewController.h"
#import "FlickrFetcher.h"

@implementation EXIFViewController

@synthesize photo;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

- (void) closeButtonClicked {
	[self dismissModalViewControllerAnimated: YES];	
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	self.title = @"EXIF Info";
	
	// Load the thumbnail (LOCAL)
	//UIImage *image = [UIImage imageNamed: [photo path]];
	
	// SERVERS
	FlickrFetcher *flickrFetcher = [FlickrFetcher sharedInstance];
	UIImage *image = [UIImage imageWithData: [flickrFetcher dataForPhotoID:photo.flickrPhotoID fromFarm:photo.flickrFarm onServer:photo.flickrServer withSecret:photo.flickrSecret inFormat: FlickrFetcherPhotoFormatSquare]];
	
	thumbImageView.contentMode = UIViewContentModeScaleAspectFit;
	thumbImageView.image = image;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   
	// Return the number of sections.
    return 3;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
	// Return the number of rows in the section (i.e. number of users)
	if(section == 0) {
		return 2;
	}
	else if (section == 1) {
		return 2;
	}
	else if (section == 2) {
		return 4;
	}
	
	//Error
	return 0;
}

// Used to place an image in the main header
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	if(section == 0) {
		return @"General";
	}
	else if (section == 1) {
		return @"Equipment";
	}
	else if (section == 2) {
		return @"Camera Settings";
	}
	else
	{
		return nil;
	}
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
	static NSString *CellIdentifier = @"EXIFCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }
	
	// SECTION 0 (General)
	if(indexPath.section == 0 && indexPath.row == 0) {
		cell.imageView.image = [UIImage imageNamed: @"exifPhoto.png"];
		cell.textLabel.text = @"Photo";
		cell.detailTextLabel.text = [photo name];
	}
	else if(indexPath.section == 0 && indexPath.row == 1) {
		cell.imageView.image = [UIImage imageNamed: @"exifPhotographer.png"];
		cell.textLabel.text = @"Photographer";
		cell.detailTextLabel.text = [[photo user] name];
	}
	else if (indexPath.section == 0 && indexPath.row > 1) {
		cell.textLabel.text = @"ERROR";
		cell.detailTextLabel.text = @"ERROR";
	}
	
	
	// SECTION 1 (Equiptment)
	else if(indexPath.section == 1 && indexPath.row == 0) {
		cell.imageView.image = [UIImage imageNamed: @"exifCamera.png"];
		cell.textLabel.text = @"Camera";
		cell.detailTextLabel.text = [photo exifCamera];
	}
	else if(indexPath.section == 1 && indexPath.row == 1) {
		cell.imageView.image = [UIImage imageNamed: @"exifLens.png"];
		cell.textLabel.text = @"Lens";
		cell.detailTextLabel.text = [photo exifLens]; 
	}
	else if(indexPath.section == 1 && indexPath.row > 1) {
		cell.textLabel.text = @"ERROR";
		cell.detailTextLabel.text = @"ERROR"; 
	}

	
	// SECTION 2 (Camera Settings)
	else if(indexPath.section == 2 && indexPath.row == 0) {
		cell.imageView.image = [UIImage imageNamed: @"exifFocalLength.png"];
		cell.textLabel.text = @"Focal Length";
		cell.detailTextLabel.text = [photo exifFocalLength];
	}
	else if(indexPath.section == 2 && indexPath.row == 1) {
		cell.imageView.image = [UIImage imageNamed: @"exifAperture.png"];
		cell.textLabel.text = @"Aperture";
		cell.detailTextLabel.text = [photo exifAperture];
	}
	else if(indexPath.section == 2 && indexPath.row == 2) {
		cell.imageView.image = [UIImage imageNamed: @"exifIso.png"];
		cell.textLabel.text = @"ISO";
		cell.detailTextLabel.text = [photo exifIso];
	}
	else if(indexPath.section == 2 && indexPath.row == 3) {
		cell.imageView.image = [UIImage imageNamed: @"exifShutter.png"];
		cell.textLabel.text = @"Shutter";
		cell.detailTextLabel.text = [photo exifShutter];
	}
	else {
		cell.textLabel.text = @"ERROR";
		cell.detailTextLabel.text = @"ERROR";
	}
	
	return cell;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@synthesize thumbImageView;
@synthesize closeLabel;
@synthesize exifTableView;
@end
